//
//  MASExampleMulLinesView.m
//  MasonryExample
//
//  Created by 山刘 on 2020/6/10.
//  Copyright © 2020 山刘. All rights reserved.
//

#import "MASExampleMulLinesView.h"

@implementation MASExampleMulLinesView
+ (BOOL)requiresConstraintBasedLayout {
    return YES;
}
- (id)init {
    self = [super init];
    if (!self) return nil;
    
    NSLog(@"%s:%@",__FUNCTION__,NSStringFromCGRect(self.frame));//-[MASExampleMulLinesView init]:{{0, 0}, {0, 0}}
    
    UIView *contentView = [LayoutUtils createView];
    [self addSubview:contentView];
    [contentView makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self).offset(@(20));
        make.left.right.equalTo(self);
    }];
    
    UIView *lastView = nil;
    
    NSInteger itemsCount = 11;
    NSInteger rowCount = 3;// 设置每行有4个Item
    NSInteger rowNum = (itemsCount/rowCount) + ((NSInteger)(itemsCount%rowCount>0));//显示的总行数
    NSInteger itemHeight = 80;
    
    for (int j = 0; j < rowNum; j++) {
        
        NSMutableArray<UILabel*> *rowArray = [NSMutableArray array];
        
        for (int i = 0; i < rowCount; i++) {
            NSInteger index = j*rowCount+i;
            UILabel *label = [LayoutUtils fixedLabelWithText:[NSString stringWithFormat:@"content:%zd",index]];
            [contentView addSubview:label];
            [rowArray addObject:label];
            lastView = label;
            
            if (index > itemsCount - 1) {
                label.text = @"";
                [label setBackgroundColor:UIColor.clearColor];
            }
        }
        if (rowArray.count > 0) {
            [rowArray mas_distributeViewsAlongAxis:MASAxisTypeHorizontal withFixedSpacing:15 leadSpacing:10 tailSpacing:10];
            [rowArray makeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(@((itemHeight+20)*j));
                make.height.equalTo(@(itemHeight));
            }];
        }
    }
    
    if (lastView) {
        [contentView makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.mas_equalTo(lastView.mas_bottom);
        }];
    }
    
    UIView *bottomView = [LayoutUtils createView];
    [self addSubview:bottomView];
    [bottomView makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(contentView.mas_bottom).offset(@(20));
        make.left.right.equalTo(self);
        make.height.equalTo(@(40));
    }];
    return self;
}
-(void)layoutSubviews{
    [super layoutSubviews];
    NSLog(@"%s:%@",__FUNCTION__,NSStringFromCGRect(self.frame));//-[MASExampleMulLinesView layoutSubviews]:{{0, 0}, {414, 808}}
}
-(void)updateConstraints{
    NSLog(@"%s:%@",__FUNCTION__,NSStringFromCGRect(self.frame));//-[MASExampleMulLinesView updateConstraints]:{{0, 0}, {414, 808}}
    [super updateConstraints];
}
@end
